//
//  VoteListViewModel.m
//  meiyinxiang
//
//  Created by 媒印象 on 05/12/2017.
//  Copyright © 2017 hbjApple. All rights reserved.
//
/*
 @property (nonatomic,strong) NSString *age;
 @property (nonatomic,strong) NSString *create_time;
 @property (nonatomic,strong) NSString *voteid;
 @property (nonatomic,strong) NSString *image;
 @property (nonatomic,strong) NSString *name;
 @property (nonatomic,strong) NSString *tel;
 @property (nonatomic,strong) NSString *uid;
 @property (nonatomic,strong) NSString *vote_num;
 */

#import "VoteListViewModel.h"

@implementation VoteViewModel
- (VoteViewModel *)initWithModel:(MYXVoteModel *)model{
    self.age = model.age;
    self.create_time = model.create_time;
    self.voteid = [NSString stringWithFormat:@"编号:%@",model.voteid];
    self.image = [NSString stringWithFormat:@"%@%@",MYXIMGBaseUrl,model.image];
    self.name = model.name;
    self.tel = model.tel;
    self.uid = model.uid;
    self.vote_num = [NSString stringWithFormat:@"%@票",model.vote_num];
    return self;
}
- (void)voteWithResult:(VoteFinishBlock)result{
    
    
    [[MYXHttpManager shared] voteWithVoteId:self.voteid Result:^(BOOL sucess, NSString *msg) {
        if (sucess) {
            result(YES);
            [SVProgressHUD showSuccessWithStatus:msg];
        }else{
            result(NO);
            ShowSVProgressErrorMessage(@"您的投票机会已用完");
           // [SVProgressHUD showErrorWithStatus:@"您的投票机会已用完"];
        }
    } errorResult:^(NSError *enginerError) {
        result(NO);
    }];
}
- (void)dosomting{
    NSLog(@"哈哈哈哈哈哈哈");
}
@end


@implementation VoteListViewModel
- (id)init{
   // [super init];
    self.page = 1;
    if (self.viewModelArray == nil) {
        self.viewModelArray = [NSMutableArray array];
    }
    if (self.searchResultViewModel == nil) {
        self.searchResultViewModel = [NSMutableArray array];
    }
    return self;
}
- (void)loadDataWithIsPullUp:(BOOL)isPullup withResult:(VotelistFinishBlock)result{
    if (isPullup) {
        self.page ++;
    }else{
        self.page = 1;
        [self.viewModelArray removeAllObjects];
    }
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [[MYXHttpManager shared] getVoteListWithPage:page size:@"10" Result:^(BOOL success, NSString *msg, NSArray *arrays) {
        if (success) {
            for (MYXVoteModel *model in arrays) {
                VoteViewModel *viewModel = [[VoteViewModel alloc] initWithModel:model];
                [self.viewModelArray addObject:viewModel];
            }
            if (arrays.count == 10) {
                result(YES);
            }else{
                result(NO);
            }
        }else{
            result(NO);
        }
        
    } errorResult:^(NSError *enginerError) {
     
        
    }];
}
- (void)searchPersonWithKeyword:(NSString *)keyword
                     withResult:(VoteFinishBlock)result{
    [[MYXHttpManager shared] applyActivitySearchWithKeyWord:keyword withResult:^(BOOL success, NSString *msg, NSArray *arrays) {
        if (success) {
            for (MYXVoteModel *model in arrays) {
                VoteViewModel *viewModel = [[VoteViewModel alloc] initWithModel:model];
                [self.searchResultViewModel addObject:viewModel];
            }
            result(YES);
        }else{
            result(NO);
        }
        
    } errorResult:^(NSError *enginerError) {
        
    }];
}
@end
